<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <title>考试管理</title>
    <link rel="stylesheet" href="<%=request.getContextPath()%>/static/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="<%=request.getContextPath()%>/static/layuiadmin/style/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">查询条件</label>
                    <div class="layui-input-block">
                        <input type="text" name="name" placeholder="请输入考试名称" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">类型</label>
                    <div class="layui-input-inline">
                        <select name="type" id="type">
                            <option value="0">请选择</option>
                            <option value="1">作业</option>
                            <option value="2">考试</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-useradmin" lay-submit lay-filter="LAY-user-front-search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                </div>
            </div>
        </div>

        <div class="layui-card-body">

            <!--数据表格-->
           <table class="layui-hide" id="demo" lay-filter="test"></table>
           <!--数据操作按钮-->
            <script type="text/html" id="barDemo">
                <a class="layui-btn layui-btn-xs" lay-event="select">查看</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
            </script>
            <!--页面按钮-->
        </div>
    </div>
</div>


<script type="text/javascript" src="<%=request.getContextPath()%>/static/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '<%=request.getContextPath()%>/static/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use('index');
    <!--使用layui功能-->
    layui.use(['index', 'form','table'], function(){
        var $ = layui.$
            ,form = layui.form
            ,table = layui.table;
        //执行一个 table 实例
        table.render({
            elem: '#demo'
            ,url: '<%=request.getContextPath()%>/examWork/list' //数据接口
            ,toolbar: '#toolbarDemo' //开启工具栏，此处显示默认图标，可以自定义模板，详见文档
            ,cols: [[ //表头
                {type:'numbers',width:60, title: '序号',fixed: 'center',align: "center"}
                ,{field: 'id', title: 'id', align:'center'}
                ,{field: 'name', title: '名称', align:'center'}
                ,{field: 'remark2', title: '课程', align:'center'}
                ,{field: 'startTime', title: '开始时间', align:'center'}
                ,{field: 'endTime', title: '结束时间', align:'center'}
                ,{field: 'remark3', title: '密码', align:'center'}
                ,{field: 'type', title: '类型', align:'center'
                    ,templet: function(d){
                        return getType(d.type);
                    }
                }
                ,{fixed: 'right', title: '操作', align:'center', toolbar: '#barDemo'}
            ]]
            ,page: true,
            text: "暂无数据！"
        });

        function getType(id){
            if(id==1){
                return '作业'
            }else{
                return '考试'
            }
        }


        //监听搜索
        form.on('submit(LAY-user-front-search)', function(data){
            var field = data.field;
            console.log(field);
            //执行重载
            table.reload('demo', {
                where: field
            });
        });

        //监听头工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id)
                ,data = checkStatus.data; //获取选中的数据
            switch(obj.event){
                case 'add':
                    layer.open({
                        type: 2,
                        title: '添加考试',
                        shadeClose: true,
                        shade: 0.8,
                        area: ['600px', '580px'],
                        content: '<%=request.getContextPath()%>/user/addUser' //iframe的url
                    });
                    break;
            };
        });

        //监听行工具事件
        table.on('tool(test)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                ,layEvent = obj.event; //获得 lay-event 对应的值
            if(layEvent === 'del'){
                layer.confirm('真的删除该考试么？', function(index){
                    layer.close(index);
                    //向服务端发送删除指令
                    $.ajax({
                        type: "post",
                        url: "<%=request.getContextPath()%>/examWork/del",
                        data: {"id":data.id},
                        dataType: "json",
                        success: function (date) {
                        }
                    });
                    layer.msg('删除成功');
                    location.reload();
                });
            }else if(layEvent === 'select'){
                layer.open({
                    type: 2,
                    title: '查看考试信息',
                    shadeClose: true,
                    shade: 0.8,
                    area: ['1200px', '600px'],
                    content: '<%=request.getContextPath()%>/examWork/selectExam?id='+data.id //iframe的url
                });
            }
        });
    });
</script>
</body>
</html>
